package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.SyncReadListener;
import com.qf.pojo.OrderSetting;
import com.qf.pojo.Result;
import com.qf.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {

    @Autowired
    private OrdersettingService ordersettingService;

    @GetMapping("/{yearMonth}") //2022-10
    public Result findMonth(@PathVariable String yearMonth){
        String beginDay = yearMonth + "-" + 1; //2022-10-1
        String endDay = yearMonth + "-" + 31; //2022-10-31

        List<OrderSetting> orderSettings = ordersettingService.findByMonth(beginDay, endDay);

        return Result.success(orderSettings);
    }

    @PostMapping
    public Result addOrUpdate(@RequestBody OrderSetting orderSetting){ //{"orderDate":"2022-11-1","number":150}
        ordersettingService.addOrUpdate(orderSetting);

        return Result.success(null);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile) throws IOException {
        //springboot项目回头发布时，以jar包形式发布，接收文件时，能否将一个文件写到项目的jar包中? 无法实现的
        //springboot项目接受文件时，一般采用外部存储方式进行存储.① 存到服务器的本地（某个目录） ② 存储到分布式文件存储系统里(七牛云、阿里云、fastdfs、minio)

        if (excelFile != null) {
            //将文件存储到当前机器的某个目录
            String baseDir = "C:\\Users\\aming\\Desktop\\uploads";

            //获取上传的文件的文件名 (ordersetting_template.xlsx)
            String originalFilename = excelFile.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //生成一个唯一文件名
            String uniqueFilename = UUID.randomUUID().toString().replaceAll("-", "") + suffix;

            String dirPath = baseDir + "\\ordersetting";
            //确保存放文件的目录是存在的
            File dirFile = new File(dirPath);
            if (!dirFile.exists()){
                dirFile.mkdirs();
            }

            //最终存放文件的文件路径
            String filePath = dirPath + "\\" + uniqueFilename;

            //保存文件
            excelFile.transferTo(new File(filePath));

            //读取上传后的文件中的内容，将数据添加/更新到ordersetting表中
            /*
                参数一File file： 要读取的目标文件
                参数二Class head ：将目标文件中的行记录封装到java的那个类型对象
                参数三ReadListener readListener ：读取过程中的回调对象(每读取一行，封装成一个对象后，都会触发这个对象的invoke方法)
             */
            List<OrderSetting> orderSettings = new ArrayList<>();
            EasyExcel.read(new File(filePath),OrderSetting.class,new SyncReadListener(){
                @Override
                public void invoke(Object object, AnalysisContext context) {
                    orderSettings.add((OrderSetting) object);
                }
            }).doReadAll();


            //更新或者添加
            for (OrderSetting orderSetting : orderSettings) {
                ordersettingService.addOrUpdate(orderSetting);
            }

            return Result.success(null);
        }


        return Result.error(Result.ERROR,"文件不能为空");
    }
}
